﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web.Security;

namespace Tebbee.Business.Server
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "PublisherSer" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select PublisherSer.svc or PublisherSer.svc.cs at the Solution Explorer and start debugging.
    public class PublisherSer : IPublisherSer
    {
        private readonly TebbeeContext context;


        public PublisherSer()
        {
            context = new TebbeeContext();
        }


        /// <summary>
        /// Get channel infomation like about, cover, name.
        /// </summary>
        /// <param name="pubid"></param>
        /// <returns>Channel type</returns>
        public Channel_GetByPubId_Result GetChanelByPubId(int pubid)
        {
            return context.Channel_GetByPubId(pubid).Single();
        }

        /// <summary>
        /// Get list advs of a publisher
        /// </summary>
        /// <param name="pubId"></param>
        /// <param name="startRow"></param>
        /// <param name="elementQ"></param>
        /// <param name="orderBy"></param>
        /// <returns>List advs</returns>
        public ICollection<Advertise_GetAdvByPublisherId_Result> GetAdvByPubId(int pubId, int startRow, int elementQ, int orderBy)
        {
            return context.Advertise_GetAdvByPublisherId(pubId,startRow,elementQ,orderBy).ToList();
        }

        /// <summary>
        /// Get channel information for current online user
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public Channel_GetCurrentChanel_Result GetCurrentChanel(string userName)
        {
            var userId = (Guid)Membership.GetUser(userName).ProviderUserKey;
            return context.Channel_GetCurrentChanel(userId).Single();
        }

        /// <summary>
        /// get list ads for a publisher
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="startRow"></param>
        /// <param name="elementQ"></param>
        /// <param name="orderBy"></param>
        /// <returns></returns>
        public ICollection<Advertise_GetPubAds_Result> GetAdvByCurrentPub(string uid, int startRow, int elementQ, int orderBy)
        {
            //var userId = (Guid)Membership.GetUser(uid).ProviderUserKey;
            var ads = context.Advertise_GetPubAds(uid, startRow, elementQ, orderBy,"").ToList();
            return ads;
        }

        /// <summary>
        /// Edit, update publisher channel
        /// </summary>
        /// <param name="channel"></param>
        /// <returns></returns>
        public int Channel_Edit(Publisher pub)
        {
            ObjectParameter RowEffected = new ObjectParameter("RowEffected", typeof(Int32));
            context.Channel_Edit(pub.PubId, pub.CompanyName, pub.Phone,pub.Facebook,pub.Address, RowEffected);
            return (int)RowEffected.Value;
        }

        //public App_Publisher_Get_Result GetPublisher(string userName)
        //{
        //    return context.App_Publisher_Get(userName).Single();
        //}

        /// <summary>
        /// Change publisher logo
        /// </summary>
        /// <param name="pubid"></param>
        /// <param name="logoLink"></param>
        public void Channel_ChangeAvatar(int pubid, string logoLink)
        {
            context.Channel_ChangeLogo(logoLink, pubid);
        }


        public void Channel_ChangeCover(int pubid, string coverLink)
        {
            context.Channel_ChangeCover(coverLink, pubid);
        }


        public ICollection<Publisher_GetSubcribers_Result> Channel_Subcribers(string pubName)
        {
            var subcribers = context.Publisher_GetSubcribers(pubName);
            return subcribers.ToList();
        }

        public ICollection<Publisher_GetNewComment_Result> Channel_NewComments(string pubName)
        {
            var comments = context.Publisher_GetNewComment(pubName);
            return comments.ToList();
        }


        public void Channel_updateAbout(string pubName, string about)
        {
            context.Channel_UpdateAbout(pubName, about);
        }

        public ICollection<Gallery_GetByPub_Result> Gallery_GetByPub(string pubName)
        {
            var gallery = context.Gallery_GetByPub(pubName);
            return gallery.ToList();
        }

        public void Gallery_insert(string pubName, int advId, string imgLink)
        {
            context.Gallery_InsertImg(pubName, imgLink, advId);
        }


        public ICollection<Gallery_GetByPubId_Result> Gallery_GetByPubId(int pubId)
        {
            var gallery = context.Gallery_GetByPubId(pubId).ToList();
            return gallery;
        }
    }
}
